// ImageUtils.h
//
// This file contains wrappers for DarkGDK image and bitmap functions, which load
// asynchronously by default, to make them synchronous (i.e., they only return
// AFTER the image is done loading)  These functions are much more efficient than
// using hard-coded sleep commands.
//
// Nick Kitten
// 2009

#pragma once
#include "DarkGDK.h"
#include "GameTime.h"

// max time to give for loading completion before returning anyway
#define MAX_LOAD_TIME 10

//////////////////////////////////////////////////////////////////
// Wrapper functions for DarkGDK image commands which only
// return after the operation is done
inline void sCreateAnimatedSprite( int ID, char * imageFile, int columns, int rows, int imageID )
{
	GameTime clock = GameTime();
	int startTime = clock.getTime();
	int elapsedTime = 0;

	dbCreateAnimatedSprite( ID, imageFile, columns, rows, imageID );

	while( ! dbSpriteExist( ID ) && elapsedTime < MAX_LOAD_TIME )
		elapsedTime = clock.getTime() - startTime;
}

inline void sCreateBitmap( int ID, int width, int height )
{
	GameTime clock = GameTime();
	int startTime = clock.getTime();
	int elapsedTime = 0;

	dbCreateBitmap( ID, width, height );

	while( ! dbBitmapExist( ID ) && elapsedTime < MAX_LOAD_TIME )
		elapsedTime = clock.getTime() - startTime;
}

inline void sLoadBitmap( char * imageFile, int ID )
{
	GameTime clock = GameTime();
	int startTime = clock.getTime();
	int elapsedTime = 0;

	dbLoadBitmap( imageFile, ID );

	while( ! dbBitmapExist( ID ) && elapsedTime < MAX_LOAD_TIME )
		elapsedTime = clock.getTime() - startTime;
}

inline void sSizeSprite( int ID, int width, int height )
{
	GameTime clock = GameTime();
	int startTime = clock.getTime();
	int elapsedTime = 0;

	dbSizeSprite( ID, width, height );

	while( dbSpriteWidth(ID) != width && elapsedTime < MAX_LOAD_TIME )
		elapsedTime = clock.getTime() - startTime;
}


// The functions below have yet to be implemented, because
// I haven't thought of a good way to do it yet.
/*
inline void PasteImage( int ID, int x, int y )
{
	dbPasteImage( ID, x, y );
	while( ! dbBitmapExist( ID ) )
		elapsedTime = clock.getTime() - startTime;
}
inline void PasteImage( int ID, int x, int y, int transparency );
inline void GetImage( int ID, int x1, int y1, int x2, int y2 );
inline void GetImage( int ID, int x1, int y1, int x2, int y2, int flag );
inline void PasteSprite( int ID, int x, int y );
*/

// A rectangle used for buttons, cameras, etc.
typedef struct Rect
{
	Position minPoint;
	Position maxPoint;
} Rect;